package com.ciwong.epaper.modules.scan.adapter;

import android.content.Context;
import android.net.TrafficStats;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.util.download.DownLoadInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 书箱目录列表adapter Created by res-jianpingliu on 2015/7/24.
 */
public class QrInfoAdapter extends BaseAdapter {
    private Context mContext;

    private List<DownLoadInfo> mList;

    //获取通过Mobile连接收到的字节总数
    private long total_data = TrafficStats.getTotalRxBytes();

    public QrInfoAdapter(Context context, List<DownLoadInfo> directionList) {
        this.mContext = context;
        if (directionList == null) {
            directionList = new ArrayList<DownLoadInfo>();
        }
        mList = directionList;
    }

    @Override
    public int getCount() {
        if (mList != null && !mList.isEmpty()) {
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mList != null && !mList.isEmpty()) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder item;
        if (convertView == null) {
            convertView = View.inflate(mContext,
                    R.layout.adapter_directory_item, null);
            item = new ViewHolder();
            item.titleTv = (TextView) convertView
                    .findViewById(R.id.directory_item_title_tv);

            convertView.setTag(item);
        } else {
            item = (ViewHolder) convertView.getTag();
        }
        DownLoadInfo info = null;
        try {
            info = mList.get(position);
        } catch (Exception e) {
            e.printStackTrace();
            notifyDataSetChanged();
            return convertView;
        }
        item.titleTv.setText(info.getChapterName());
        return convertView;
    }



    private class ViewHolder {
        TextView titleTv;
        ImageView noIm;
    }

    /**
     * 得到当前网速
     *
     * @return
     */
    private long getNetSpeed() {
        long traffic_data = TrafficStats.getTotalRxBytes() - total_data;
        total_data = TrafficStats.getTotalRxBytes();
        return traffic_data;
    }



}
